package com.hiss.basic.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.log.annotation.Log;
import com.hiss.basic.starter.log.enums.BusinessTypeEnum;
import com.hiss.basic.model.system.pojo.AuthenticationOnlineModel;
import com.hiss.basic.model.system.request.AuthenticationOnlineQueryRequest;
import com.hiss.basic.system.service.cache.AuthenticationOnlineCache;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统会话 controller
 */
@RestController
@RequestMapping("/sys/session")
public class SysSessionController {

    @Resource
    private AuthenticationOnlineCache authenticationOnlineCache;

    @SaCheckPermission("sys:session:list")
    @RequestMapping(value = "/list/page", method = RequestMethod.GET)
    public R<Page<AuthenticationOnlineModel>> listPage(AuthenticationOnlineQueryRequest request) {
        return R.success(authenticationOnlineCache.listPage(request));
    }

    @SaCheckPermission("sys:session:forced-offline")
    @Log(title = "强制下线", businessType = BusinessTypeEnum.FORCE)
    @RequestMapping(value = "/forced-offline", method = RequestMethod.GET)
    public R<Void> forcedOffline(String token) {
        StpUtil.kickoutByTokenValue(token);
        return R.success();
    }
}
